/*
*  NSRTMTask.m
*  ApiTest
*
*  Created by kkillian on 17/11/09.
*  Copyright 2009 shufflecodebox. All rights reserved.
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*
*	You should have received a copy of the GNU General Public License
*	along with this program; if not, write to the Free Software
*	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#import "NSRTMTask.h"
#import "DebugLog.h"

@implementation NSRTMTask

-(id)init {
	self = [super init];
	if(self != nil) {
		//[self addObserver:self forKeyPath:@"iscompleted" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionPrior) context:NULL];
		[self addObserver:self forKeyPath:@"completed" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
	}
	return self;
}

/*+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
	
	NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
	
	DLog(@"KIAVE");
	if ([key isEqualToString:@"iscompleted"])
		
    {
		
        NSSet *affectingKeys = [NSSet setWithObjects:@"completed", nil];
		
        keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
		
    }
	
	return keyPaths;
}*/

-(NSString *)getId {
	return task_id;
}
-(NSDate *)getDue {
	return [NSDate dateFromISO8601:due];
}
-(NSString *)getHasDueTime {
	return has_due_time;
}
-(NSDate *)getAdded {
	return [NSDate dateFromISO8601:added];
}
-(NSDate *)getCompleted {
	return [NSDate dateFromISO8601:completed];
}

-(void)setCompleted:(NSString *)datevalue {
	[self willChangeValueForKey:@"completed"];
	// DLog(@"CHIAMATO setCompleted (%@)",datevalue);
	completed = datevalue;
	[self didChangeValueForKey:@"completed"];
}

-(NSDate *)getDeleted {
	return [NSDate dateFromISO8601:deleted];
}
-(NSString *)getPriority {
	if([priority isEqualToString:@"N"]) {
		priority = @"";
	}
	return priority;
}
-(NSString *)getPostponed {
	return postponed;
}
-(NSString *)getEstimate {
	return estimate;
}

-(NSInteger)isCompleted {
	// DLog(@"CHIAMATO isCompleted: %d",iscompleted);
	return iscompleted;
}

-(void)setIsCompleted:(NSInteger)completedValue {
	[self willChangeValueForKey:@"iscompleted"];
	// ... change data here	
	// DLog(@"CHIAMATO setIsCompleted: %d",completedValue);
	iscompleted = completedValue;
	[self didChangeValueForKey:@"iscompleted"];
}




- (void)observeValueForKeyPath:(NSString *)keyPath

					  ofObject:(id)object

                        change:(NSDictionary *)change

                       context:(void *)context

{

	// DLog(@"CHIAMATO per %@: Old = %@ and New = %@",keyPath,[change objectForKey:NSKeyValueChangeOldKey],[change objectForKey:NSKeyValueChangeNewKey]);
	if([keyPath isEqualToString:@"completed"]) {
		if(completed != nil && [completed length] > 0) {
			// DLog(@"COMPLETED DATE NOT NULL");
			[self setIsCompleted:1];
		} else {
			[self setIsCompleted:0];
		}
			
	}
	
	if([keyPath isEqualToString:@"iscompleted"]) {
		if([change objectForKey:NSKeyValueChangeNewKey]) {
			// [self setIsCompleted:YES];
		} else {
			// [self setIsCompleted:NO];
		}
		
	}
	
	
	
		// be sure to call the super implementation
	
		// if the superclass implements it
	
    	
}

@end
